﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Security;

namespace Common
{
    public class StrUtil
    {
        public static string SubString(string str, int len)
        {
            if (str != null)
            {
                return str.Length > len ? GetStrLenB(str, 0, len) + "..." : str + "...";
            }
            return "";
        }

        //字符是否为汉字
        private static bool IsChinese(char c)
        {
            return c >= 0x4E00 && c <= 0x9FA5;
        }

        //获得字节长度
        private static int GetLengthb(string str)
        {
            return Encoding.Default.GetByteCount(str);
        }

        //中英文混合字符串截取指定长度
        private static string GetStrLenB(string str, int startidx, int len)
        {
            int lengthb = GetLengthb(str);
            if (startidx + 1 > lengthb)
            {
                return "";
            }

            int j = 0;
            int l = 0;
            bool b = false;
            string rstr = "";

            for (int i = 0; i < str.Length; i++)
            {
                char c = str[i];
                if (j >= startidx)
                {
                    rstr = rstr + c;
                    b = true;
                }

                int strw = IsChinese(c) ? 2 : 1;

                j = j + strw;

                if (!b) continue;
                l = l + strw;
                if ((l + 1) >= len) break;
            }

            return rstr;
        }

        public static int GetStringCount(string s)
        {
            return !string.IsNullOrEmpty(s) ? s.Length : 0;
        }

        public static string UserMd5(string str)
        {
            string result = str;
            for (int i = 1; i <= 5; i++)
            {
                if (result != null) result = FormsAuthentication.HashPasswordForStoringInConfigFile(result, "MD5");
                if (result != null) result = result.ToLower();
            }
            return result;
        }

        public string GetCode(string un)
        {
            //  return System.Text.Encoding.GetEncoding("iso8859-1").GetString(System.Text.Encoding.GetEncoding("gb2312").GetBytes(srcString));   
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            string un2 = Encoding.UTF8.GetString(gb2312.GetBytes(un));
            //un2 = Encoding.Default.GetString(gb2312.GetBytes(un));
            // un2 = Encoding.Default.GetString(Encoding.UTF8.GetBytes(un));
            //un2 = Encoding.Default.GetString(latin.GetBytes(un));
            //un2 = Encoding.ASCII.GetString(Encoding.UTF8.GetBytes(un));
            // un2 = Encoding.ASCII.GetString(gb2312.GetBytes(un));
            // un2 = Encoding.ASCII.GetString(Encoding.Default.GetBytes(un));
            return un2;
        }

        #region 功能：过滤Html

        /// <summary>
        /// 过滤Html
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string RemoveHtml(string html)
        {
            html = html.Replace("<BR>", " ");
            var regex1 = new Regex(@"<script[\s\S]+</script *>", RegexOptions.IgnoreCase);
            var regex2 = new Regex(@" href *= *[\s\S]*script *:", RegexOptions.IgnoreCase);
            var regex3 = new Regex(@" no[\s\S]*=", RegexOptions.IgnoreCase);
            var regex4 = new Regex(@"<iframe[\s\S]+</iframe *>", RegexOptions.IgnoreCase);
            var regex5 = new Regex(@"<frameset[\s\S]+</frameset *>", RegexOptions.IgnoreCase);
            var regex6 = new Regex(@"\<img[^\>]+\>", RegexOptions.IgnoreCase);
            var regex7 = new Regex(@"</p>", RegexOptions.IgnoreCase);
            var regex8 = new Regex(@"<p>", RegexOptions.IgnoreCase);
            var regex9 = new Regex(@"<[^>]*>", RegexOptions.IgnoreCase);
            html = regex1.Replace(html, ""); //过滤<script></script>标记 
            html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性 
            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 
            html = regex4.Replace(html, ""); //过滤iframe 
            html = regex5.Replace(html, ""); //过滤frameset 
            html = regex6.Replace(html, ""); //过滤frameset 
            html = regex7.Replace(html, ""); //过滤frameset 
            html = regex8.Replace(html, ""); //过滤frameset 
            html = regex9.Replace(html, "");
            html = html.Replace("&nbsp;", " ");
            html = html.Replace("&lt;", "<");
            html = html.Replace("&gt;", ">");
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            return html;
        }

        public static string Transcoding(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return "";
            }
            text = Uri.EscapeDataString(text);
            //var s = "";
            //foreach (var v in text)
            //{
            //    if(v.ToString() == "%")
            //    {
            //        s += "％";
            //    }
            //    else
            //    {
            //        s += v.ToString();
            //    }
            //}
            text = text.Replace("%", "％");
            return text;
        }

        public static string UnTranscoding(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return "";
            }
            text = text.Replace("％", "%");
            text = Uri.UnescapeDataString(text);
            return text;
        }

        #endregion
    }
}